package com.mef.smsplat.eterm;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.mef.smsplat.eterm.bean.DetrResult;
import com.mef.smsplat.eterm.util.EtermUtil;

public class Detr extends AComm {
	
	public Detr(){
		super.setEterm(new Eterm());
	}
	
	public Detr(IEterm eterm){
		super.setEterm(eterm);
	}
	
	/**
	 * 根据票号提取
	 * @param tktNo
	 * @return
	 */
	public DetrResult detrByTN(String tktNo){
		StringBuilder sb=new StringBuilder();
		try {
			getEterm().connect();//建立连接
			String content =super.sendCmd("DETR:TN/"+tktNo);
			sb.append(content+spliter);
			String lastPn = content;
			//检测收到的结果
			int maxCount = 200;
			while(EtermUtil.checkNeedPn(content) || maxCount<=0)
			{
				maxCount--;
				//发送PN指令
				content=super.sendCmd("PN");
				
				//判断翻页结果是否还和上次一样，如果一样，则证明翻到底了 
				if(lastPn != null && lastPn.equals(content)) 
					break;
				//如果没有翻到底，则记录下来
				sb.append(content+spliter);
				lastPn = content;
			}
			

		} catch (Exception e) {
			logger.error("发送或接受指令出错",e);
		}finally{
			try {
				getEterm().close();//关闭连接
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
		String ret=sb.toString().replaceAll("\\+", "");
		logger.debug("从Eterm返回的结果是：\n"+ret);
		return DetrUtil.parseDetr(ret);
	}
	
	/**
	 * 根据证件号提取
	 * @param ni
	 * @return
	 */
	public List<DetrResult> detrByNI(String ni){
		return null;
	}
	
	/**
	 * 根据姓名提取
	 * @param nm
	 * @return
	 */
	public List<DetrResult> detrByNM(String nm){
		return null;
	}
	
	public static void main(String[] args) {
		Detr detr=new Detr(new Eterm());
		DetrResult r=detr.detrByTN("088-2140082225");
		//System.out.println("结果："+new String(r.g));
		//System.out.println("asldj-lkjl".replaceAll("-", "A"));
	}
}
